package cn.com.zsw.gblog.modules.gblog.controller;

import cn.com.zsw.gblog.config.cache.RedisCache;
import cn.com.zsw.gblog.constants.SysConstants;
import cn.com.zsw.gblog.modules.gblog.dto.PostsPageDTO;
import cn.com.zsw.gblog.modules.gblog.dto.PostsSaveDTO;
import cn.com.zsw.gblog.modules.gblog.service.GbPostsService;
import cn.com.zsw.gblog.vo.R;
import com.baomidou.mybatisplus.extension.api.ApiController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * (GbPosts)表控制层
 *
 * @author shiwangzhou
 * @since 2020-06-22 11:35:30
 */
@RestController
@RequestMapping("blog/posts")
@Api(value = "GbPostsController", tags = "博客")
public class GbPostsController extends ApiController {

    @Resource
    RedisCache redisCache;
    /**
     * 服务对象
     */
    @Resource
    private GbPostsService gbPostsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("博客详情")
    @GetMapping("view")
    public R view(String id) {
        //有service接口有cacheable注解,所以把他提到controller层
        redisCache.hashIncr(SysConstants.POST_VIEW_KEY, id);
        return R.SUCCESS.data(this.gbPostsService.view(id));
    }

    @ApiOperation("博客详情")
    @GetMapping("edit")
    public R edit(String id) {
        return R.SUCCESS.data(this.gbPostsService.edit(id));
    }

    @PostMapping("page")
    @ApiOperation("分页查询")
    public R paging(@RequestBody PostsPageDTO postsPageDTO) {
        return R.SUCCESS.data(gbPostsService.selectListPage(postsPageDTO));
    }

    @PostMapping("save")
    @ApiOperation("发布博客")
    public R save(@RequestBody PostsSaveDTO postsSaveDTO) {
        gbPostsService.savePost(postsSaveDTO);
        return R.SUCCESS.msg("保存成功");
    }


}